﻿using NLog;
using NLog.Fluent;
using System.Diagnostics;

namespace TraceLog
{
    public class TraceLogListener : TraceListener
    {
        public static Logger logger = LogManager.GetLogger("SimpleDemo");

        public TraceLogListener()
        {

        }

        public TraceLogListener(string name)
        {
            logger = LogManager.GetLogger(name);
        }

        public override void Write(string message)
        {
            logger.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    ") + message);
        }

        public override void Write(object o, string message)
        {
            TraceLogType? tt =o as TraceLogType;
            if (tt != null)
            {
                if (tt.LogType == WriteLogType.info)
                    logger.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    ") + tt.ClassName.ToString() + "    " + message);
                if (tt.LogType == WriteLogType.info)
                    logger.Error(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    ") + tt.ClassName.ToString() + "    " + message);            }
            else
            {
                logger.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    ") + message);
            }    
        }

        public override void WriteLine(string message)
        {
            logger.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    ") + message + Environment.NewLine);
        }

        public override void WriteLine(object o, string message)
        {
            TraceLogType? tt = o as TraceLogType;
            if (tt != null)
            {
                if (tt.LogType == WriteLogType.info)
                    logger.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    ") + tt.ClassName.ToString() + "    " + message + Environment.NewLine);
                if (tt.LogType == WriteLogType.info)
                    logger.Error(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    ") + tt.ClassName.ToString() + "    " + message + Environment.NewLine);
            }
            else
            {
                logger.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss    ") + message + Environment.NewLine);
            }
        }
    }
}